<?php

namespace App\Http\Controllers\CRM\MiniAPP;

use App\Enums\MiniAPP\DuelStatus;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Resources\MiniAPP\DuelResource;
use App\Models\Duel;
use Illuminate\Http\Request;

class DuelController extends Controller
{
    public function getDuelList(Request $request)
    {
        $query = Duel::latest()->with([
            'member', 'opponent',
            'tableType', 'duelType', 'duelLevel', 'store',
        ]);

        if (!empty($request->status))
            $query = $query->where('status', $request->status);

        $duels = $query->paginate();

        return DuelResource::collection($duels);
    }

    public function reviewSuccess(Duel $duel)
    {
        if ($duel->status != DuelStatus::PendingReview)
            return ApiResponseHelper::json([],400, '约战状态不正确');

        $duel->status = DuelStatus::InviteSuccess;
        $duel->save();

        return ApiResponseHelper::json([]);
    }

    public function reviewFail(Duel $duel)
    {
        if ($duel->status != DuelStatus::PendingReview)
            return ApiResponseHelper::json([],400, '约战状态不正确');

        $duel->status = DuelStatus::InviteFail;
        $duel->save();

        return ApiResponseHelper::json([]);
    }
}
